<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="regex.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.regex.html">POSIX Regex 函数</a></div>
 <div class="up"><a href="book.regex.html">POSIX Regex</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="regex.examples" class="appendix">
 <h1>范例</h1>

 <p class="para">
  <div class="example" id="example-4708">
  <p><strong>Example #1 Regular Expression Examples</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Returns&nbsp;true&nbsp;if&nbsp;"abc"&nbsp;is&nbsp;found&nbsp;anywhere&nbsp;in&nbsp;$string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Returns&nbsp;true&nbsp;if&nbsp;"abc"&nbsp;is&nbsp;found&nbsp;at&nbsp;the&nbsp;beginning&nbsp;of&nbsp;$string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"^abc"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Returns&nbsp;true&nbsp;if&nbsp;"abc"&nbsp;is&nbsp;found&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;$string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc$"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Returns&nbsp;true&nbsp;if&nbsp;client&nbsp;browser&nbsp;is&nbsp;Netscape&nbsp;2,&nbsp;3&nbsp;or&nbsp;MSIE&nbsp;3.<br /></span><span style="color: #0000BB">eregi</span><span style="color: #007700">(</span><span style="color: #DD0000">"(ozilla.[23]|MSIE.3)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"HTTP_USER_AGENT"</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;Places&nbsp;three&nbsp;space&nbsp;separated&nbsp;words&nbsp;into&nbsp;$regs[1],&nbsp;$regs[2]&nbsp;and&nbsp;$regs[3].<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"([[:alnum:]]+)&nbsp;([[:alnum:]]+)&nbsp;([[:alnum:]]+)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$regs</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Put&nbsp;a&nbsp;&lt;br&nbsp;/&gt;&nbsp;tag&nbsp;at&nbsp;the&nbsp;beginning&nbsp;of&nbsp;$string.<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"^"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Put&nbsp;a&nbsp;&lt;br&nbsp;/&gt;&nbsp;tag&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;$string.<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"$"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;rid&nbsp;of&nbsp;any&nbsp;newline&nbsp;characters&nbsp;in&nbsp;$string.<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="regex.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.regex.html">POSIX Regex 函数</a></div>
 <div class="up"><a href="book.regex.html">POSIX Regex</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
